<?php
#+------------------------------------------------------------------
#| 我曾七次鄙视自己的灵魂:
#| 第一次,当它本可进取时，却故作谦卑；
#| 第二次,当它空虚时，用爱欲来填充；
#| 第三次,在困难和容易之间，它选择了容易；
#| 第四次,它犯了错，却借由别人也会犯错来宽慰自己；
#| 第五次,它自由软弱，却把它认为是生命的坚韧；
#| 第六次,当它鄙夷一张丑恶的嘴脸时，却不知那正是自己面具中的一副；
#| 第七次,它侧身于生活的污泥中虽不甘心，却又畏首畏尾。
#+------------------------------------------------------------------
#| Author:Janmas Cromwell <janmas-cromwell@outlook.com>
#+------------------------------------------------------------------
namespace app\common\lib\email;

use think\facade\Cache;

class Email
{
    public const BIND = 'bind';
    public const LOGIN = 'login';
    public const REGISTER = 'register';
    public const VERIFY = 'verify';
    public $sign = '';

    public const SCENE = [
        'verify' => [
            'title' => '[#SIGN#]邮件验证通知',
            'content' => '[#SIGN#]欢迎您！您的验证码是:%s,此验证码10分钟内有效'
        ],
        'login' => [
            'title' => '[#SIGN#]登录验证码通知',
            'content' => '您正在登录[#SIGN#]，你的验证码是:%s,此验证码10分钟内有效',
        ],
        'register' => [
            'title' => '[#SIGN#]注册验证码通知',
            'content' => '您正在注册[#SIGN#]，你的验证码是:%s,此验证码10分钟内有效',
        ],
        'bind' => [
            'title' => '[#SIGN#]邮箱绑定通知',
            'content' => '[#SIGN#]您正在绑定邮箱,您的验证码是：%s,此验证码10分钟内有效',
        ]
    ];

    public function __construct(string $signName = '墨盘')
    {
        $this->sign = $signName;
    }


    public function send(string $email, string $title, string $content)
    {
//        validate(['email' => 'email|require'], ['email.email' => '邮箱格式错误', 'email.require' => '请输入邮箱'])->failException(true)->check(['email' => $email]);
        $configKey = ['debug','host','port','smtp','charset','auth','username','passwd','nickname'];
        $config = [];
        foreach ($configKey as $value){
            $config[$value] = env('email.'.$value);
        }
        $emailClass = new EmailManage($config);
        return $emailClass->send($email, $title, $content);
    }

    public function sendVerifyCode(string $email, string $scene = 'verify')
    {
        if (!in_array($scene, array_keys(self::SCENE), true)) {
            return false;
        }

        $title = str_replace('#SIGN#', $this->sign, self::SCENE[$scene]['title']);
        $code = rand(1000, 9999);
        $content = sprintf(self::SCENE[$scene]['content'], $code);
        $content = str_replace('#SIGN#', $this->sign, $content);

        if ($this->send($email, $title, $content)) {
            Cache::set($scene . '_' . $email, $code, 60 * 10);
            return true;
        }
        return false;
    }

    public function verify(int $code, string $email = '', string $scene = 'verify')
    {
        validate(['email' => 'email|require'], ['email.email' => '邮箱格式错误', 'email.require' => '请输入邮箱'])->failException(true)->check(['email' => $email]);
        if (!Cache::has($scene . '_' . $email)) {
            return false;
        }

        $res = Cache::get($scene . '_' . $email) == $code;
        Cache::delete($scene . '_' . $email);
        return $res;
    }
}
